<template>
  <div style="position: relative;">
    <el-form :inline="true" size="mini" :model="formInline" label-position="right">
      <el-form-item label="企业名称">
        <el-input class="filter-item" v-model="formInline.enterpriseName" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="单位属性">
        <el-select v-model="formInline.unitAttribute" placeholder="请选择">
          <el-option label="全部" value="0"></el-option>
          <el-option label="国有企业" value="1"></el-option>
          <el-option label="国有控股企业" value="2"></el-option>
          <el-option label="外资企业" value="3"></el-option>
          <el-option label="合资企业" value="4"></el-option>
          <el-option label="私营企业" value="5"></el-option>
          <el-option label="事业单位" value="6"></el-option>
          <el-option label="国家行政机关" value="7"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="所在地区">
        <el-cascader v-model="areaCodes"
          ref="ref_areaCodes"
          :options="areaCodelist"
          :props="areaCodeprops"
          :show-all-levels="false"
          clearable
          @change="areaCodesChange"></el-cascader>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="mini" @click="onSubmit">搜索</el-button>
        <el-button type="warning" class="el-button--reset" icon="el-icon-refresh-right" @click="resetFromdata" size="mini"></el-button>
        <el-button type="primary" size="mini" @click="addFromdata">创建</el-button>
      </el-form-item>
    </el-form>
    <el-row>
      <el-col :span="24">
        <el-table :data="tableData" size="mini" style="width: 100%;" header-cell-class-name="table-header-gray" v-loading="isShowloading">
          <el-table-column type="index" align="center" width="55" label="序号"></el-table-column>
          <el-table-column prop="enterpriseName" align="center" label="企业名" show-overflow-tooltip></el-table-column>
          <el-table-column prop="unitAttributeName" align="center" label="单位属性" show-overflow-tooltip></el-table-column>
          <el-table-column prop="qualificationLevel" align="center" label="资质等级" show-overflow-tooltip>
          </el-table-column>
          <el-table-column prop="districtName" align="center" label="所在地区" show-overflow-tooltip></el-table-column>
          <el-table-column prop="manager" align="center" label="负责人" show-overflow-tooltip></el-table-column>
          <el-table-column prop="custPost" align="center" label="职务" show-overflow-tooltip></el-table-column>
          <el-table-column prop="telephone" align="center" label="手机号" show-overflow-tooltip></el-table-column>
          <el-table-column prop="createUserName" align="center" label="创建人" show-overflow-tooltip></el-table-column>
          <el-table-column prop="createTime" align="center" label="创建时间" show-overflow-tooltip></el-table-column>
          <el-table-column label="操作" align="center" width="180">
            <template slot-scope="scope">
              <el-button type="text" @click="handleInfo(scope.row)">查看</el-button>
              <el-button type="text" @click="handleEdit(scope.row)">修改</el-button>
              <el-button type="text" @click="handleDelete(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-row type="flex" justify="end" style="margin-top: 10px;">
          <el-pagination
            background
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="pageNo"
            :page-size="page_size"
            :page-sizes="[10, 20, 50, 100]"
            layout="total, prev, pager, next, sizes"
            :total="total"></el-pagination>
        </el-row>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { EcologicalkKnowledge } from '@/api/knowledgeBases.js';
import Ecologicaladd from './Ecologicaladd.vue';
import EcologicalDetail from './EcologicalDetail.vue';
export default {
  name: 'Ecologicalknowledge',
  data () {
    return {
      editDialogVisible: false,
      formInline: {
        enterpriseName: null,
        unitAttribute: null,
        districtCode: null
      },
      tableData: [],
      total: 0,
      page_size: 10,
      pageNo: 1,
      isShowloading: false,
      areaCodelist: [],
      areaCodes: [],
      areaCodeprops: {
        label: 'name',
        value: 'id',
        children: 'children',
        checkStrictly: true
      }
    }
  },
  mounted () {
    this.loadknowledgeWidgetData()
    this.loadareaCodelistDict()
  },
  computed: {},
  methods: {
    /**
     * 行政区划
     */
    loadareaCodelistDict () {
      return new Promise((resolve, reject) => {
        EcologicalkKnowledge.listDict(this).then(res => {
          let areaCodelist = res.data
          let areaCodelistnew = []
          areaCodelist.forEach((item, index) => {
            if (!item.parentId) {
              item.children = []
              areaCodelistnew.push(item)
            }
          })
          areaCodelistnew.forEach((item1, index1) => {
            let info = areaCodelist.find((item2, index2) => {
              return item1.id === item2.parentId
            })
            if (info) {
              item1.children = []
              areaCodelist.forEach((item3, index3) => {
                if (item1.id === item3.parentId) {
                  item1.children.push(item3)
                }
              })
            } else {
              item1.children = null
            }
          })
          areaCodelistnew.forEach((item4, index4) => {
            if (item4.children) {
              item4.children.forEach((item5, index5) => {
                let info2 = areaCodelist.find((item6, index6) => {
                  return item5.id === item6.parentId
                })
                if (info2) {
                  item5.children = []
                  areaCodelist.forEach((item7, index7) => {
                    if (item5.id === item7.parentId) {
                      item5.children.push(item7)
                    }
                  })
                } else {
                  item5.children = null
                }
              })
            }
          })
          this.areaCodelist = areaCodelistnew
        }).catch(e => {
          reject(e);
        });
      });
    },

    /**
     * 选择行政区划
     */
    areaCodesChange () {
      let info = this.areaCodes
      let length = info.length - 1
      this.formInline.districtCode = info[length]
      this.$refs['ref_areaCodes'].dropDownVisible = false
    },
    /**
     * 知识库
     */
    loadknowledgeWidgetData () {
      let params = {
        ...this.formInline,
        size: this.page_size,
        current: this.pageNo
      }
      return new Promise((resolve, reject) => {
        EcologicalkKnowledge.list(this, params).then(res => {
          this.tableData = res.data.records
          this.total = res.data.total
        }).catch(e => {
          reject(e);
        });
      });
    },
    // 搜索
    onSubmit () {
      this.pageNo = 1
      this.loadknowledgeWidgetData()
    },
    // 重置
    resetFromdata () {
      this.formInline = {
        name: null,
        businessType: null,
        catalogType: null,
        deptId: null
      }
      this.loadknowledgeWidgetData()
    },
    // 分页导航
    handleCurrentChange (val) {
      this.pageNo = val
      this.loadknowledgeWidgetData()
    },
    handleSizeChange (val) {
      this.page_size = val
      this.loadknowledgeWidgetData()
    },
    currentChange (val) {
      this.pageNo = val
      this.loadknowledgeWidgetData()
    },
    // 创建生态库
    addFromdata () {
      let params = {};
      this.$dialog.show('新建', Ecologicaladd, {
        area: '800px'
      }, params).then(res => {
        this.loadknowledgeWidgetData();
      }).catch(e => {});
    },
    // 查看
    handleInfo (row) {
      let params = {
        EcologicalId: row.id
      };
      this.$dialog.show('详情', EcologicalDetail, {
        area: '800px'
      }, params).then(res => {
        this.loadknowledgeWidgetData()
      }).catch(e => {
      });
    },
    // 修改
    handleEdit (row) {
      let params = {
        EcologicalId: row.id
      };
      this.$dialog.show(row ? '编辑' : '新建', Ecologicaladd, {
        area: '800px'
      }, params).then(res => {
        this.loadknowledgeWidgetData()
      }).catch(e => {
      });
    },
    // 删除
    handleDelete (row) {
      this.$confirm('确定要删除吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let params = row.id
        return new Promise((resolve, reject) => {
          EcologicalkKnowledge.delete(this, params).then(res => {
            this.loadknowledgeWidgetData()
          }).catch(e => {
            reject(e);
          });
        });
      })
    }
  }
}
</script>
